package com.dq.health.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dq.health.dto.request.common.BaseIdRequest;
import com.dq.health.dto.request.prescription.QueryPrescriptionParam;
import com.dq.health.dto.request.prescription.SavePrescriptionRequest;
import com.dq.health.dto.response.prescription.PrescriptionDetailInfo;
import com.dq.health.dto.response.prescription.PrescriptionListInfo;
import com.dq.health.service.IAPIPrescriptionService;
import com.dq.health.utils.SecurityUtils;
import com.dq.health.utils.data.JsonUtil;
import com.dq.health.utils.view.SimpleView;
import com.dq.health.utils.view.StringView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@RestController
@RequestMapping("/prescription")
@Api(tags = "处方单控制器")
public class PrescriptionOrderController {

    private static final Logger LOGGER = LoggerFactory.getLogger(PrescriptionOrderController.class);

    @Autowired
    private IAPIPrescriptionService prescriptionService;

    @PostMapping("addPrescriptionOrder")
    @ApiOperation("新增处方单")
    public SimpleView<Long> addPrescriptionOrder(@RequestBody SavePrescriptionRequest request) {
        LOGGER.info("### 新增处方单 start req:{}", JsonUtil.toString(request));
        Long loginUserId = SecurityUtils.getLoginUserId();
        request.setOperationUserId(loginUserId);

        Long id = prescriptionService.addPrescriptionOrder(request);

        SimpleView<Long> view = new SimpleView<>();
        view.success(id);
        return view;
    }

    @PostMapping("updatePrescriptionOrder")
    @ApiOperation("编辑处方单")
    public StringView updatePrescriptionOrder(@RequestBody SavePrescriptionRequest request) {
        StringView view = new StringView();

        Long loginUserId = SecurityUtils.getLoginUserId();
        request.setOperationUserId(loginUserId);
        prescriptionService.updatePrescriptionOrder(request);

        view.success("修改成功");
        return view;
    }

    @PostMapping("queryPrescriptionList")
    @ApiOperation("处方单列表")
    public SimpleView<IPage<PrescriptionListInfo>> queryPrescriptionList(@RequestBody QueryPrescriptionParam param) {
        LOGGER.info("### 处方单列表 start req:{}", JsonUtil.toString(param));
        SimpleView<IPage<PrescriptionListInfo>> view = new SimpleView<>();

        IPage<PrescriptionListInfo> prescriptionListInfoIPage = prescriptionService.queryPrescriptionList(param);
        view.success(prescriptionListInfoIPage);

        return view;
    }

    @PostMapping("getPrescriptionDetail")
    @ApiOperation("处方单详情")
    public SimpleView<PrescriptionDetailInfo> getPrescriptionDetail(@RequestBody @Valid BaseIdRequest request) {
        LOGGER.info("### 处方单详情 start req:{}", JsonUtil.toString(request));
        SimpleView<PrescriptionDetailInfo> view = new SimpleView<>();

        PrescriptionDetailInfo prescriptionDetail = prescriptionService.getPrescriptionDetail(request.getId());
        view.success(prescriptionDetail);

        return view;
    }

}
